﻿using System;
using System.Collections.ObjectModel;
using System.Data.Services.Client;
using System.Linq;
using System.Reflection;
using DevComponents.WPF.Controls;
using Microsoft.Practices.Prism.Commands;
using ZhiHeng.Consume.DataContract;
using ZhiHeng.OneCard.CommUtility;
using ZhiHeng.OneCard.WpfClient.Model;
namespace ZhiHeng.OneCard.WpfClient.ViewModel
{
    public class SuperMktViewModel : ViewModelBase
    {
        public SuperMktViewModel()
        {
            _superData = new ObservableCollection<mercacc>();
            this.SelectedSuper = new mercacc();
            GetSuper();

        }
        public void GetSuper()
        {
            SuperData = new ObservableCollection<mercacc>(new mercacc().ExtGetAll());
        }

        #region 属性
        private decimal _bFB;
        /// <summary>
        /// BFB
        /// </summary>
        public decimal BFB
        {
            get
            {
                return _bFB;
            }
            set
            {
                _bFB = value;
                this.RaisePropertyChanged("BFB");
            }
        }

        private ObservableCollection<mercacc> _superData;
        public ObservableCollection<mercacc> SuperData
        {
            get { return _superData; }
            set
            {
                _superData = value;
                this.RaisePropertyChanged("SuperData");
            }
        }
        #region 商户类型
        private ObservableCollection<Model.CardType> _comboxSource = new ObservableCollection<Model.CardType>();
        /// <summary>
        ///  ComboxSource
        /// </summary>
        public ObservableCollection<Model.CardType> ComboxSource
        {
            get
            {
                if (this._comboxSource.Count == 0)
                {
                    SuperMKTType st = new SuperMKTType();
                    _comboxSource = st.GetDefaultSuperType();
                    SelectData = _comboxSource[0];
                }
                return this._comboxSource;
            }
            set
            {
                _comboxSource = value;
                this.RaisePropertyChanged("ComboxSource");
            }
        }
        private Model.CardType _selectData;
        /// <summary>
        ///  下拉框选择
        /// </summary>
        public Model.CardType SelectData
        {
            get { return _selectData; }
            set
            {
                _selectData = value;
                this.RaisePropertyChanged("SelectData");
            }
        }
        private object _selectData1;
        /// <summary>
        ///  下拉框选择
        /// </summary>
        public object SelectData1
        {
            get { return _selectData1; }
            set
            {
                _selectData1 = value;
                this.RaisePropertyChanged("SelectData1");
            }
        }
        #endregion
        #region 商户科目
        private ObservableCollection<Model.CardType> _subSource = new ObservableCollection<Model.CardType>();
        /// <summary>
        ///  SubSource
        /// </summary>
        public ObservableCollection<Model.CardType> SubSource
        {
            get
            {
                if (this._subSource.Count == 0)
                {
                    SuperMKTType st = new SuperMKTType();
                    _subSource = st.GetSuperSub();
                    SelectSub = _subSource[0];

                }
                return this._subSource;
            }
            set
            {
                _subSource = value;
                this.RaisePropertyChanged("SubSource");
            }
        }
        private Model.CardType _selectSub;
        /// <summary>
        ///  商户科目下拉框选择
        /// </summary>
        public Model.CardType SelectSub
        {
            get { return _selectSub; }
            set
            {
                _selectSub = value;
                this.RaisePropertyChanged("SelectSub");
            }
        }
        private object _selectSub1;
        /// <summary>
        ///  商户科目下拉框选择
        /// </summary>
        public object SelectSub1
        {
            get { return _selectSub1; }
            set
            {
                _selectSub1 = value;
                this.RaisePropertyChanged("SelectSub1");
            }
        }
        #endregion

        private mercacc _selectedSuper;
        public mercacc SelectedSuper
        {
            get
            {
                if (_selectedSuper !=null && _selectedSuper.Account != 0)
                {
                    SelectedSuper1 = _selectedSuper.DeepClone();
                    BFB = SelectedSuper1.DiscountRate * 0.01m;
                    SelectData = SelectedSuper1.Type == 0 ? ComboxSource.First() : new Model.CardType() { CardTypeID = SelectedSuper1.Type, CardTypeName = ComboxSource.Where(x => x.CardTypeID.ToString() == SelectedSuper1.Type.ToString()).First().CardTypeName };
                    SelectData1 = SelectData.CardTypeID;
                    SelectSub = SelectedSuper1.Subject == "" ? SubSource.First() : new Model.CardType() { CardTypeID = SelectedSuper1.Subject, CardTypeName = SubSource.Where(x => x.CardTypeID.ToString() == SelectedSuper1.Subject.ToString()).First().CardTypeName };
                    SelectSub1 = SelectSub.CardTypeID;
                }
                return _selectedSuper;
            }
            set
            {
                _selectedSuper = value;
                this.RaisePropertyChanged("SelectedSuper");

            }
        }
        private mercacc _selectedSuper1 = new mercacc();
        public mercacc SelectedSuper1
        {
            get { return _selectedSuper1; }
            set
            {
                _selectedSuper1 = value;
                this.RaisePropertyChanged("SelectedSuper1");
            }
        }
        #endregion
        #region 事件
        private DelegateCommand _insertCommand;
        public DelegateCommand InsertCommand
        {
            set
            {
                _insertCommand = value;
                this.RaisePropertyChanged("InsertCommand");
            }
            get
            {
                if (_insertCommand != null) return _insertCommand;
                this._insertCommand = new DelegateCommand(Insert);
                return _insertCommand;
            }
        }

        private void Insert()
        {
            try
            {
                if (SelectData == null)
                {
                    ShowMsg("请选择商户类型！");
                    return;
                }
                mercacc wm = SelectedSuper1.DeepClone();
                wm.Type = (int) SelectData.CardTypeID;
                wm.DiscountRate = Convert.ToInt32(BFB*100);
                wm.OperCode = "###";
                wm.Subject = SelectSub.CardTypeID.ToString();
                wm.OpenDate = DateTime.Now;
                wm.SchCode = "01";
                wm.ExtAdd();

                SuperData = new ObservableCollection<mercacc>(new mercacc().ExtGetAll());
                ShowMsg("添加商户成功！");
            }
            catch (DataServiceRequestException dex)
            {
                ShowMsg("添加商户失败！Error：" + dex.Message.ToString(), false);
            }
            catch (Exception ex)
            {
                ShowMsg("添加商户失败！Error：" + ex.Message.ToString(), false
                    );
            }
            finally
            {
                SelectedSuper = new mercacc();
            }
        }

        private DelegateCommand _updateCommand;
        public DelegateCommand UpdateCommand
        {
            get
            {
                if (_updateCommand != null) return _updateCommand;
                this._updateCommand = new DelegateCommand(Update);
                return _updateCommand;
            }
        }

        private void Update()
        {
            try
            {
                var Dsp = new mercacc().ExtGetSingle("it.Account == @Account", false,
                                                     new SearchParameter("Account", SelectedSuper1.Account));
                if (Dsp == null)
                {
                    return;
                }


                Dsp.Address = SelectedSuper1.Address;
                if (SelectData != null)
                    Dsp.Type = (int) SelectData.CardTypeID;
                Dsp.DiscountRate = Convert.ToInt32(BFB*100);
                Dsp.Linkman = SelectedSuper1.Linkman;
                Dsp.Subject = SelectSub.CardTypeID.ToString();
                Dsp.MercCode = SelectedSuper1.MercCode;
                Dsp.MercName = SelectedSuper1.MercName;
                Dsp.OperCode = SelectedSuper1.OperCode;
                Dsp.Phone = SelectedSuper1.Phone;
                Dsp.Zip = SelectedSuper1.Zip;
                Dsp.ExtUpdate();

                ShowMsg("修改商户成功！");

            }
            catch (Exception ex)
            {
                ShowMsg("修改商户失败！Error：" + ex.Message.ToString(), false);
            }
            finally
            {
                GetSuper();
                SelectedSuper = new mercacc();
            }
        }

        private DelegateCommand _deleteCommand;
        public DelegateCommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    this._deleteCommand = new DelegateCommand(() =>
                    {
                        try
                        {
                            SelectedSuper.ExtDelete(); 
                            SuperData = new ObservableCollection<mercacc>(new mercacc().ExtGetAll());
                            ShowMsg("删除商户成功！");

                        }
                        catch (Exception ex)
                        {
                            ShowMsg("删除商户失败！Error：" + ex.Message.ToString(), false);
                        }
                        finally
                        {
                            SelectedSuper = new mercacc();
                        }
                    });
                }
                return _deleteCommand;
            }
        }

        public DelegateCommand OkCommand
        {
            get { return MainViewModel.Instance.ReturnToStartCommand; }
        }

        #endregion
    }

}
